#include "pool.hpp"
#include "thread.hpp"
#include "consumer_thread.hpp"
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
namespace rocket 
{

pool::pool(int pool_capacity,int task_list_capacity)
:_thread_pool()
,_pool_capacity(pool_capacity)
,_list(task_list_capacity)
,_exit_signal(false)
{
     
}
pool::~pool() 
{

}

void pool::add_task(elemtype2 task)
{
    _list.push(task);
}
void pool::start()
{
for (int i=0;i<_pool_capacity;++i)
{
    std::unique_ptr<consumer_thread>p(new consumer_thread(this));
    _thread_pool.push_back(std::move(p));
    _thread_pool.at(i)->start();
}
}
void pool::take_task()
{
    
    while(_exit_signal==false||_list.empty()==false)
    {
        elemtype2 temp=_list.pop(); 
        
        if(temp==nullptr)
        {
            return;
        }
	    temp();
    }


}
void pool::stop()
{
    _exit_signal=true;
    _list.notify_all();
    for(int i=0;i<_pool_capacity;++i)
    {
        cout<<"join:"<<i<<endl;
        _thread_pool.at(i)->join();
    } 
    cout<<"stop"<<endl;


}

}





